<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class UserorderController extends Controller
{
    /**
     *  用户订单处理
     */
    public function getIndex(Request $request)
    {   
        // 接受搜索的关键字
        $search = $request -> input('search','');

        // 查询当前用户的订单信息
        $res = DB::table('my_order') -> where('subuser','=',session('homeusername')) -> where('goodsname','like','%'.$search.'%') -> paginate(5);

        // 查询商品表的信息
        $goods= DB::table('goods_basis') -> get();

        $data = [];
        // 将两个表的信息拼接成一个数组
        foreach($res as $v){
            foreach ($goods as  $va) {
                if($v['gid'] == $va['id']){
                   
                    $data[] = array_merge ($va,$v);
                }
            }
        }
        // dd($data);
        // 查询这个订单在评论表evaluate里有没有数据

        
        // $arr = DB::table('evaluate') -> where('oid','=',$data[2]['id']) -> count();
        // dd($arr);

        //加载模板
        return view('home.detail.userorder',['res'=>$res,'data'=>$data,'search'=>$search]);
    }

    /**
    *   用户订单删除
    */
    public function getDelete(Request $request)
    {
        // 接受要删除的订单Id
        $id = $request -> input('id');

        // 删除订单表的信息
        $res = DB::table('my_order') -> where('id','=',$id) -> delete();

        // 判断处理结果
        if ($res > 0) {

            // 删除订单日志表数据
            $res1 = DB::table('my_order_log') -> where('pid','=',$id) -> delete();

            return redirect('/home/userorder/index') -> with('success','删除成功'); 
        } else {
            return back() -> with('error','删除失败');
        }
    }

    /**
    *   用户订单申请退款
    */
    public function getRefund(Request $request)
    {
        // 接受申请退款的订单ID和状态码
        $id = $request -> input('id');
        $merstatus = $request -> input('merstatus');
        $time = time();

        // 修改数据库里面的订单状态
        $arr = DB::table('my_order') -> where('id','=',$id) -> update(['merstatus'=>$merstatus,'errctime'=>$time]);

        // 判断处理结果
        if ($arr > 0) {
            $res = DB::table('my_order') -> where('id','=',$id) -> first();
            // 往数据库插入状态变更
            $data = ['pid'=>$res['id'],'customer'=>$res['subuser'],'customerid'=>$res['subid'],'seller'=>$res['merchant'],'sellerid'=>$res['merid'],'time'=>$time,'content'=>'买家申请退款中','status'=>4];
            DB::table('my_order_log') -> insert($data);
            
            return redirect('/home/userorder/index');
        } else {
            return back();
        }

    }

    /**
    *   用户确认收货
    */
    public function getReceipt(Request $request)
    {
        // 接受确认收货的订单ID和状态码
        $id = $request -> input('id');
        $merstatus = $request -> input('merstatus');
        $time = time();
        
        // 查询未修改的信息
        $array = DB::table('my_order') -> where('id','=',$id) -> first();

        // 修改数据库里面的订单状态
        $arr = DB::table('my_order') -> where('id','=',$id) -> update(['merstatus'=>$merstatus,'errctime'=>$time]);


        // 判断处理结果
        if ($arr > 0) {
            
            $res = DB::table('my_order') -> where('id','=',$id) -> first();
            // 往数据库插入状态变更
            if ($array['merstatus'] == 1) {
                $data = ['pid'=>$res['id'],'customer'=>$res['subuser'],'customerid'=>$res['subid'],'seller'=>$res['merchant'],'sellerid'=>$res['merid'],'time'=>$time,'content'=>'交易完成','status'=>0];
            } elseif($array['merstatus']  == 3) {
                $data = ['pid'=>$res['id'],'customer'=>$res['subuser'],'customerid'=>$res['subid'],'seller'=>$res['merchant'],'sellerid'=>$res['merid'],'time'=>$time,'content'=>'退款成功','status'=>0];
            }

            DB::table('my_order_log') -> insert($data);
            
            return redirect('/home/userorder/index');
        } else {
            return back();
        }
    }

    /**
    *   订单详情
    */
    public function getOrderdetail(Request $request)
    {
        $id = $request -> input('id');

        $data = DB::table('my_order_log') -> where('pid','=',$id) -> orderBy('time','asc') -> get();

        return view('home.detail.orderdetail',['data'=>$data]);
    }
}
